package cc.comac.mom.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.alibaba.druid.support.json.JSONUtils;
import cc.comac.mom.bean.BusLog;
import cc.comac.mom.entity.BusLogEntity;
import cc.comac.mom.redis.IdFactory;
import cc.comac.mom.util.ConfigUtil;



/**
    * @ClassName: LogAsyncServiceImpl
    * @Description: TODO(这里用一句话描述这个类的作用)
    * @author Administrator
    * @date 2018年8月29日
    *
    */
@Service
@Async
public class LogAsyncServiceImpl implements LogAsyncService {


	
	@Autowired
	private BusLogService busLogService;
	@Autowired
	private IdFactory idFactory;
	private Boolean storage=true;
	
//	private boolean storage=Boolean.parseBoolean(ConfigUtil.getValue("cafe.log.storage"));
	
	private MomLogger logger = MomLoggerFactory.getLogger(getClass());

	@Override
	public void sendLog(BusLog logInfo) {
		try {
			logger.info(logInfo.toString());
			if(storage){
				BusLogEntity busLogEntity=new BusLogEntity();
				
				String tableName = logInfo.getTableName();
				
				busLogEntity.setId(idFactory.generateId());
				busLogEntity.setLevel(logInfo.getLevel());
				busLogEntity.setMsg(logInfo.getMsg());
				busLogEntity.setRemark(logInfo.getRemark());
				busLogEntity.setServiceName(logInfo.getServiceName());
				busLogEntity.setCategory(logInfo.getCategory());
				busLogEntity.setClientIp(logInfo.getClientIp());
				busLogEntity.setUserName(logInfo.getUserName());
				busLogEntity.setCreationTime(logInfo.getCreationTime());
				int save=0;
				if(tableName==null) {
					save = busLogService.save(busLogEntity);
				}else {
					save = busLogService.save(busLogEntity,tableName);
				}
				if(save<1) {
					logger.error(logInfo.toString()+":save error");
				}
			}
		} catch (Exception e) {
			throw new RuntimeException("log.save.error：" + e.getMessage());
        }
	}
	

}
